package com.sky.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.admin.EmployeeMapper;
import com.sky.pojo.Employee;
import com.sky.result.PageResult;
import com.sky.service.admin.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();//

        //1、根据用户名查询数据库中的数据  select * from employee where username = #{username}
        Employee employee = employeeMapper.getByUsername(username); //

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        //3、返回实体对象
        return employee;
    }


    //管理员添加员工
    @Override
    public void insertEmployeeService(EmployeeDTO employeeDTO) {
        Employee employee=new Employee();
        BeanUtil.copyProperties(employeeDTO,employee);
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        employeeMapper.insertEmployee(employee);

    }


    //查询员工界面并进行分页查询还要根据员工姓名进行模糊查询
    @Override
    public PageResult selectEmployeeService(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());

        log.info(employeePageQueryDTO.getName());

        List<Employee> employees = employeeMapper.selectEmployee(employeePageQueryDTO.getName());

        log.info(String.valueOf(employees.get(1).getUpdateTime()));



        Page<Employee> employees1 = (Page<Employee>) employees;
        log.info(String.valueOf(employees1.get(1).getUpdateTime()));



        return new PageResult(employees1.getTotal(), employees1.getResult());
    }


    //根据禁用按钮传过来的请求来修改状态
    @Override
    public void updateEmployeeStatusService(Integer status, Integer id) {
        employeeMapper.updateEmployeeStatus(status, id);

    }


    //根据前端请求传来id查询员工信息回显----用于修改

    @Override
    public EmployeeDTO selectEmployeeIdService(Integer id) {
        EmployeeDTO employeeDTO = employeeMapper.selectEmployeeId(id);
        return employeeDTO;
    }


    //根据id进行修改员工信息-！修改时间-!修改人
    @Override
    public void updateEmployeeService(EmployeeDTO employeeDTO) {
        Employee employee=new Employee();
        BeanUtil.copyProperties(employeeDTO,employee);

        employeeMapper.updateEmployee(employee);

    }
}
